/*
 * 	$Id: $
 */
package org.rcgwt.client.ui.grid.internal.js;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.rcgwt.client.ui.grid.internal.IntIterator;

/**
 * 
 * @author Olivier Oeuillot (latest modification by $Author: $)
 * @version $Revision: $ $Date: $
 */
public class IntSet {

    private final Set<Integer> values;

    public IntSet(int... ints) {
        values = new HashSet<Integer>(ints.length);

        for (int v : ints) {
            values.add(new Integer(v));
        }
    }

    IntSet(Set<Integer> set) {
        if (set != null) {
            this.values = new HashSet<Integer>(set);

        } else {
            this.values = new HashSet<Integer>();
        }
    }

    public boolean contains(int value) {
        return values.contains(new Integer(value));
    }

    public void add(int... ints) {
        for (int v : ints) {
            values.add(new Integer(v));
        }
    }

    public int[] toArray() {
        int array[] = new int[values.size()];

        int i = 0;
        for (Integer index : values) {
            array[i++] = index.intValue();
        }
        Arrays.sort(array);
        return array;
    }

    public boolean isEmpty() {
        return values.isEmpty();
    }

    public IntIterator iterator() {
        return new IntIteratorDefault(values.iterator());
    }

    private static class IntIteratorDefault implements IntIterator {
        private final java.util.Iterator<Integer> it;

        public IntIteratorDefault(Iterator<Integer> it) {
            this.it = it;
        }

        public int next() {
            return it.next().intValue();
        }

        public boolean hasNext() {
            return it.hasNext();
        }
    }

    public void remove(int key) {
        values.remove(new Integer(key));
    }

    public void removeAll(IntSet set) {
        values.removeAll(set.values);
    }
}
